package com.dalianpai.gulimall.order.service.impl;

import com.dalianpai.gulimall.order.entity.OrderReturnApplyEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dalianpai.common.utils.PageUtils;
import com.dalianpai.common.utils.Query;

import com.dalianpai.gulimall.order.dao.OrderItemDao;
import com.dalianpai.gulimall.order.entity.OrderItemEntity;
import com.dalianpai.gulimall.order.service.OrderItemService;


@RabbitListener(queues={"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    @RabbitHandler
    //consumer消费到的消息包括消息header+消息body，我们真正的消息是放到消息body中的
    public void recieveMessage(Message message,
                               OrderReturnApplyEntity content,
                               Channel channel){
        System.out.println("接受的消息。。"+content);
        System.out.println(content.getSkuName());
        //channel内按顺序自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag==>"+deliveryTag);
        //签收
        try {
            channel.basicAck(deliveryTag,false);//不是批量签收
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}